package com.odm.communitymanage.ui.activity

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.odm.communitymanage.core.bean.MyActivities
import com.odm.communitymanage.core.bean.MyActivityData
import com.odm.communitymanage.core.net.ApiService
import com.odm.watercard.base.BaseViewModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import rxhttp.wrapper.param.RxHttp

/**
 * @description: 活动模块 ViewModel
 * @author: ODM
 * @date: 2020/4/20
 */
class ActivitiesViewModel : BaseViewModel(){

    var disposable : Disposable ?= null

    private var _activityList : MutableLiveData<List<MyActivityData>> = MutableLiveData()
    val activityList : LiveData<List<MyActivityData>> = _activityList




    var selectedTabIndex : Int = 0


    fun getNewestActivityList(){
        disposable = RxHttp.get(ApiService.ALL_ACTIVITY)
            .asClass(MyActivities::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                _activityList.value = it.data
            },{
                it?.let {
                    LogUtils.e(it.message)
                    it.printStackTrace()
                }
            })
    }


    fun getOngoingList(){
        disposable = RxHttp.get(ApiService.ALL_ACTIVITY)
            .asClass(MyActivities::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                val ongoingList:MutableList<MyActivityData> = mutableListOf()
                it?.let {
                    //获取列表中正在进心的活动
                    for(item in it.data){
                        if(item.status == 0){
                            ongoingList.add(item)
                        }
                    }
                    _activityList.value = ongoingList
                }
            },{
                it?.let {
                    LogUtils.e(it.message)
                    it.printStackTrace()
                }
            })
    }


    fun getOverList(){
        disposable = RxHttp.get(ApiService.ALL_ACTIVITY)
            .asClass(MyActivities::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                it?.let {
                    val overList:MutableList<MyActivityData> = mutableListOf()
                    for(item in it.data){
                        if(item.status == 1){
                            overList.add(item)
                        }
                    }
                    _activityList.value = overList
                }
            },{
                it?.let {
                    LogUtils.e(it.message)
                    it.printStackTrace()
                }
            })
    }

    fun searchActivityList(content : String ){
        disposable = RxHttp.postJson(ApiService.SEARCH_ACTIVITY)
            .add("content",content)
            .asClass(MyActivities::class.java)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe({
                it?.let {
                    _activityList.value = it.data
                }
            },{
                it?.let {
                    LogUtils.e(it.message)
                    it.printStackTrace()
                }

            })
    }

}